package com.heima.behavior.service.impl;

import com.heima.behavior.dtos.UnLikesBehaviorDto;
import com.heima.behavior.pojos.ApUnlikesBehavior;
import com.heima.behavior.service.UnLikesBehaviorService;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.model.user.pojos.ApUser;
import com.heima.utils.thread.ApThreadLocalUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
 * @author DiXiaoNie
 * @since 2023/1/3 19:25
 */
@Service
public class UnLikesBehaviorServiceImpl implements UnLikesBehaviorService {

    @Autowired
    private StringRedisTemplate redisTemplate;

    @Autowired
    private MongoTemplate mongoTemplate;

    /**
     * 不喜欢
     *
     * @param dto dto
     * @return {@link ResponseResult }
     * @author DiXiaoNie
     * @date 2023/01/03
     */
    @Override
    public ResponseResult unLikes(UnLikesBehaviorDto dto) {

        //1.参数校验
        if (dto == null || dto.getArticleId() == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        //2.获取用户登录ID
        ApUser apUser = ApThreadLocalUtil.getApUserThreadLocal();
        //判断是是否是游客登录
        if (apUser == null || apUser.getId() == 0) {
            return ResponseResult.errorResult(AppHttpCodeEnum.NEED_LOGIN);
        }
        //  type   * 0 不喜欢
        //     * 1 取消不喜欢
        //查看当前用户是否不喜欢过
        String unLikeKey = "USER_UNLIKE_" + dto.getArticleId();
        Boolean unLikeFlag = this.redisTemplate.opsForHash().hasKey(unLikeKey, apUser.getId().toString());
        //如果不喜欢过 而此时的操作是不喜欢操作 type = 0 直接返回
        if (unLikeFlag && dto.getType() == 0) {
            return ResponseResult.errorResult(5001, "您已不喜欢过此作品，请勿重复操作");
        }
        // 如果没有不喜欢过，而此时的操作是取消不喜欢操作 type = 1 直接返回
        if (!unLikeFlag && dto.getType() == 1) {
            return ResponseResult.errorResult(5001, "不可执行此操作");
        }
        // 如果不喜欢过 而此时是取消不喜欢操作 type = 1 删除redis和mongo
        if (unLikeFlag && dto.getType() == 1) {
            //删除redis中的数据
            this.redisTemplate.opsForHash().delete(unLikeKey, apUser.getId().toString());
            //删除mongo中的数据'
            this.mongoTemplate.remove(Query.query(
                    Criteria.where("userId").is(apUser.getId()).and("articleId").is(dto.getArticleId())
            ), ApUnlikesBehavior.class);
            return ResponseResult.okResult("取消不喜欢成功");
        }
        //如果没有不喜欢过 而此时是不喜欢操作 type = 0 添加redis和mongo
        if (!unLikeFlag && dto.getType() == 0) {
            //添加redis数据
            this.redisTemplate.opsForHash().put(unLikeKey, apUser.getId().toString(), "1");
            //添加mongo数据
            ApUnlikesBehavior apUnlikesBehavior = new ApUnlikesBehavior();
            BeanUtils.copyProperties(dto, apUnlikesBehavior);
            apUnlikesBehavior.setUserId(apUser.getId().longValue());
            apUnlikesBehavior.setCreatedTime(new Date());
            this.mongoTemplate.insert(apUnlikesBehavior);
        }
        return ResponseResult.okResult("不喜欢成功");
    }
}
